package com.controller;


import com.bean.Commodity;
import com.service.CommodityService;
import com.util.PageAssistant;
import com.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Controller
@RequestMapping("commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    /**
     * 查询所有商品信息
     *
     */
    @RequestMapping("query")
    public String query(HttpSession session, Model model, PageAssistant<Commodity> paCommodity, Commodity commodity){
        System.out.println(">>>>>>>>> commodity query");
        Map<String,Object> map=new HashMap<>();

        map.put("start",paCommodity.getStart());
        map.put("size",paCommodity.getSize());

        paCommodity.setCount(commodityService.count(map));
        paCommodity.setItems(commodityService.query(map));

        model.addAttribute("paCommodity",paCommodity);
        return "page/commodity/commodityList";

    }

    /**
     * 添加商品信息
     *
     */
    @RequestMapping("add")
    public String add() {return "page/commodity/commodityadd";}

    /**
     *
     * @return
     */
    @RequestMapping("save")
    public String save(HttpSession session, Model model, CommodityVo commodity){
        System.out.println(">>>>>> commodity save");
        System.out.println("commodity:"+commodity);
        System.out.println("------------------");
        commodity.setSpare(" ");

        int save = commodityService.save(commodity.parse());
        System.out.println(save);
        if(save != 1) return null;

        return "redirect:/commodityList?cdyName="+commodity.getCdyName();
    }


    /**
     * 删除商品
     */
    @RequestMapping("delete")
    public String delete(Commodity commodity){
        int remove=commodityService.remove(commodity);

        if(remove ==1){
            return "redirect:commodity/query";
        }
        return null;
    }

    @RequestMapping("see")
    public String modify(Model model,Commodity commodity){
        System.out.println(">>>>>> commodity see");
        System.out.println("commodity:"+commodity);
        System.out.println("------------------");
        List<Commodity> commoditySee =commodityService.find(commodity);
        if(commoditySee.size()==1){
            model.addAttribute("commoditySee",commoditySee.get(0));
            return "page/commodity/commoditymodify";
        }
        return null;
    }

    /**
     * 修改商品
     */
    @RequestMapping("update")

    public  String modify(HttpSession session, CommodityVo commodity){
        System.out.println(">>> user update");
        System.out.println(commodity);
        System.out.println("------------------------------------------------");

        int count=commodityService.modify(commodity.parse());

        String updateMsg="修改成功";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "修改失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "redirect:/commodity/unique?id="+commodity.getId();

    }
}
